package cn.com.sinux.spring.timing;

import cn.com.sinux.spring.commons.SpringUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Map;

/**
 * Created by Administrator on 2016/10/7.
 */
public class QuartzJobBean  implements Job
{

    public QuartzJobBean()
    {
    }

    public void execute(JobExecutionContext context)
            throws JobExecutionException
    {
        String targetBeanId = (String)context.getMergedJobDataMap().get("serviceTag");
        if(StringUtils.isEmpty(targetBeanId))
            return;
        Object targetBean = SpringUtils.getBean(targetBeanId);
        if(null == targetBean)
            return;
        if(!(targetBean instanceof QuartzJob))
            return;
        Map args = (Map)context.getMergedJobDataMap().get("args");
        QuartzJob job = (QuartzJob)targetBean;
        try
        {
            job.execute(args);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
